feat: Add SQLAlchemy models for Chancy tables#83
Conversation
This adds a new contrib module with SQLAlchemy models that mirror the existing Django models. Users can now query Chancy data using SQLAlchemy's ORM without managing the table schema. New files: - chancy/contrib/sqlalchemy/__init__.py - Module docstring - chancy/contrib/sqlalchemy/models.py - Job, Worker, Queue models - tests/contrib/sqlalchemy/test_models.py - Integration tests The models support: - Table prefix customization via CHANCY_PREFIX environment variable - Full SQLAlchemy 2.0 style with mapped_column and Mapped types - PostgreSQL-specific types (JSONB, ARRAY, UUID) - Comprehensive docstrings with usage examples Closes TkTech#75
|
|
||
| with Session(engine) as session: | ||
| # Get all pending jobs | ||
| pending_jobs = session.query(Job).filter(Job.state == "pending").all() |
There was a problem hiding this comment.
nit: this documentation uses the former SQLA syntaxe, i.e. session.query.
test_models.py uses modern syntaxe session.execute though.
| PG_UUID(as_uuid=True), | ||
| primary_key=True, | ||
| ) | ||
| queue: Mapped[str] = mapped_column(Text, nullable=False) |
There was a problem hiding this comment.
nit: For the whole file, nullable can be inferred from the type.
| queue: Mapped[str] = mapped_column(Text, nullable=False) | |
| queue: Mapped[str] = mapped_column(Text) |
If Optional is used, nullable=True is inferred.
| """ | ||
|
|
||
| __tablename__ = f"{PREFIX}jobs" | ||
| __table_args__ = {"extend_existing": True} |
There was a problem hiding this comment.
Pull request overview
This PR introduces a new chancy.contrib.sqlalchemy contrib module providing SQLAlchemy ORM models intended to map onto Chancy’s existing PostgreSQL tables (similar to the existing Django unmanaged models), along with integration tests to validate basic querying.
Changes:
- Added
chancy.contrib.sqlalchemymodule and initial ORM model definitions forJob,Queue, andWorker. - Added integration tests that create Chancy records and query them via SQLAlchemy.
- Added module-level documentation and usage examples.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 20 comments.
| File | Description |
|---|---|
chancy/contrib/sqlalchemy/__init__.py |
Adds module documentation for the SQLAlchemy contrib package. |
chancy/contrib/sqlalchemy/models.py |
Introduces SQLAlchemy declarative models intended to match Chancy core tables. |
tests/contrib/sqlalchemy/test_models.py |
Adds integration tests to validate ORM querying against the Chancy test database. |
tests/contrib/sqlalchemy/__init__.py |
Adds test package init file for SQLAlchemy contrib tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Create a queue using Chancy | ||
| await chancy.push_queue( | ||
| chancy.Queue( | ||
| name="test-sqlalchemy-queue", | ||
| concurrency=5, | ||
| polling_interval=10, | ||
| ) | ||
| ) |
| # Ensure queue exists | ||
| await chancy.push_queue( | ||
| chancy.Queue( | ||
| name="test-job-queue", | ||
| concurrency=1, | ||
| ) | ||
| ) |
| # Ensure queue exists | ||
| await chancy.push_queue( | ||
| chancy.Queue( | ||
| name="test-filter-queue", | ||
| concurrency=1, | ||
| ) | ||
| ) |
| """Test Job model __repr__ method.""" | ||
| await chancy.push_queue( | ||
| chancy.Queue(name="repr-test-queue", concurrency=1) | ||
| ) |
| """Test Queue model __repr__ method.""" | ||
| await chancy.push_queue( | ||
| chancy.Queue(name="repr-queue", concurrency=1) | ||
| ) |
| return create_engine( | ||
| "postgresql://postgres:localtest@localhost:8190/postgres" | ||
| ) |
| job = await chancy.push( | ||
| chancy.Job.from_func( | ||
| "tests.contrib.sqlalchemy.test_models.dummy_job", | ||
| queue="test-job-queue", | ||
| kwargs={"message": "hello"}, | ||
| ) |
| await chancy.push( | ||
| chancy.Job.from_func( | ||
| "tests.contrib.sqlalchemy.test_models.dummy_job", | ||
| queue="test-filter-queue", | ||
| kwargs={"index": i}, | ||
| ) |
| job = await chancy.push( | ||
| chancy.Job.from_func( | ||
| "tests.contrib.sqlalchemy.test_models.dummy_job", | ||
| queue="repr-test-queue", | ||
| ) |
| import pytest | ||
| from sqlalchemy import create_engine, select | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from chancy.contrib.sqlalchemy.models import Base, Job, Queue, Worker | ||
|
|
Summary
This PR adds SQLAlchemy models as a new contrib module, mirroring the existing Django models. This addresses #75.
Changes
Features
mapped_columnandMappedtypesCHANCY_PREFIXenvironment variableUsage Example
Closes #75